$(".news_editor_tiny").before("<div class='news_editor_tiny_loading' style='width:100%; height: 400pxl; line-height: 400px; background: rgb(242,242,242); font-size: 15px;text-align: center'>加载中...</div>");
var tinyEditor=tinymce.init({
  selector: '.news_editor_tiny',
  language:'zh_CN',
  plugins: 'print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help emoticons searchreplace autoresize help importword wxarticle aidog',
  height: 650, //编辑器高度
  min_height: 500,
  toolbar_sticky: true,//当编辑器内容比较长需要滚动的时候，编辑器工具栏会固定在顶部
  branding: false,
  toolbar: ' undo redo | formatselect | bold italic underline forecolor backcolor strikethrough | fontselect fontsizeselect lineheight  alignleft aligncenter alignright alignjustify outdent indent  | bullist numlist  |table|  removeformat | cut copy paste pastetext|  blockquote subscript superscript |image media  link  charmap emoticons  hr | pagebreak insertdatetime | importword wxarticle_button | kityformula-editor | print remove code searchreplace preview fullscreen help aidog_button' ,
  toolbar_mode : 'wrap',//显示所有工具栏
  font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;',
  //注意这里一定要加上，否则在有些环境上用ajax（save_form）保存的数据有问题
  setup: function (editor) {
      editor.on('init', function () {
          // 当编辑器初始化完成后，隐藏loading元素，显示编辑器容器
          $(".news_editor_tiny_loading").remove();
      });
    editor.on('change', function () {
      editor.save();
        clearTimeout(saveTimeout);
        saveTimeout = setTimeout(function() {
          saveFormData();
        }, 1000);
    });
  },
  fontsize_formats: "12px 14px 16px 18px 20px 22px 24pt 36pt",     
  convert_urls:false,//禁止转换url
  paste_data_images: true, // 默认是false的，记得要改为true才能粘贴
  images_upload_handler: function (blobInfo, success, failure, progress) {
      var xhr, formData;
      xhr = new XMLHttpRequest();
      xhr.withCredentials = false;
      //pubid
      var this_pubid = $("input[name='pubid']").val();
      this_pubid=this_pubid?this_pubid:1;
      var listtype="img";
      xhr.open('POST', upload_add+'?listtype=img&pubid='+this_pubid);
      xhr.upload.onprogress = function(e){
          progress(e.loaded / e.total * 100);
      }
      xhr.onload = function() {
          var json;
          if (xhr.status == 403) {
              failure('HTTP Error: ' + xhr.status, { remove: true });
              return;
          }
          if (xhr.status < 200 || xhr.status >= 300 ) {
              failure('HTTP Error: ' + xhr.status);
              return;
          }
          json = JSON.parse(xhr.responseText);
          if (!json || json.status != 1) {
              failure( json.info);
              return;
          }
          success(json.filepath);
      };

      xhr.onerror = function () {
          failure('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
      }

      formData = new FormData();
      formData.append('file', blobInfo.blob(), blobInfo.filename());
      xhr.send(formData);
  },
  file_picker_callback: function(callback, value, meta) {
      if (meta.filetype == 'file') {
          selectfiles('',function(result){
              callback(result[1].file_path, {text: result[1].file_name});
          },"file")
      }
      if (meta.filetype == 'image') {
          var that=this;
          selectfiles('',function(result){
                  var html='';
                  for (var i = 1; i < result.length; i++) {
                      var file_name = result[i]['file_name'];
                      var file_path = result[i]['file_path'];
                      if(file_path){
                          html += '<img src="'+file_path+'" alt="'+file_name+'" />';
                      }
                  }
                  //如果选择了多张图片
                  if(i>2){
                      that.insertContent(html);//插入html
                      that.windowManager.close();//关闭窗口  
                  }else{
                      callback(file_path, {alt:file_name});
                  }
          },"img")
      }
      // Provide alternative source and posted for the media dialog
      if (meta.filetype == 'media') {
          selectfiles('',function(result){
              callback(result[1].file_path, {source2: result[1].file_name,poster:''});
          },"video")            
      }
  },  
  //导入word
    importword_handler: function(editor,files,next){
        var file_name = files[0].name
        if(file_name.substr(file_name.lastIndexOf(".")+1)=='docx'){
            editor.notificationManager.open({
                text: '正在转换中...',
                type: 'info',
                closeButton: false,
            });
             next(files);
        }else{
            editor.notificationManager.open({
                text: '目前仅支持docx文件格式，若为doc111，请将扩展名改为docx',
                type: 'warning',
            });
        }
    },
    importword_filter: function(result,insert,message){ 
       // 自定义操作部分
      insert(result) //回插函数
    }
});